<template>
  <div>

    <el-dialog
      width="30%"
      title="选择流程"
      :visible.sync="checkVisible"
    >
      <compTree
        ref="Tree"
        :tree-show-checkbox="true"
        :tree-data="treeData"
        :highlight-current="true"
        :tree-height="400"
        :show-search="false"
        :tree-default-checked-keys="treeDefaultCheckedKeys"
        @treeNodeCheck="treeNodeCheck"
      />
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          size="mini"
          @click="dialogClose"
        >取 消</el-button>
        <el-button
          size="mini"
          type="primary"
          @click="dialogConfirm"
        >确 定</el-button>
      </span>
    </el-dialog>
    <select-move-group ref="selectMoveGroup" :select-params="selectParams" />
    <user-confirm-layer ref="userconfirmlayer" />
  </div>
</template>
<script>
import { getRpaFlowList, batchDelFlow, batchMoveFlow } from '@/api/flow'
import Global from '@/../public/js/global'
import { ParamArr, SQLTable } from '@/../public/js/paramarr'
import SelectMoveGroup from './SelectMoveGroup.vue'
import UserConfirmLayer from './UserConfirmLayer.vue'
export default {
  name: 'SelectflowLayer',
  components: { SelectMoveGroup, UserConfirmLayer },
  props: {
    // eslint-disable-next-line vue/require-default-prop
    params: {
      type: Object
    }
  },
  data() {
    return {
      checked: false,
      checkVisible: false,
      formData: '',
      treeData: [],
      checkedNodes: '',
      nodeId: '', // 保存分组ID,
      selectParams: {},
      elementData: [],
      treeDefaultCheckedKeys: []
    }
  },
  methods: {
    open(ok) {
      this.formData = this._.cloneDeep(this.params)
      this.ok = ok
      this.checkVisible = true
      getRpaFlowList(new ParamArr()).then(vPar => {
        this.lpfInitWindow(vPar)
      })
      if (this.formData[1].fsLimitMutexID !== '') {
        this.treeDefaultCheckedKeys = this.formData[1].fsLimitMutexID.split(',')
      }
    },
    treeNodeCheck(a, b) {
      this.checkedNodes = b.checkedNodes
    },
    lpfInitWindow(vPar) {
      var nodes = []
      var nodeMap = new Map()
      // 分组
      var table = new SQLTable(vPar.getTable('k_flow_group'))
      table.first()
      while (!table.eof()) {
        var pnode = Global.getTreeviewPNode(nodeMap, nodes, table.getValueByName('PID'))
        var node = {}
        // data
        node['id'] = table.getValueByName('ID')
        node['name'] = table.getValueByName('Name')
        node['text'] = node.name
        node['icon'] = 'folder'
        node['type'] = 0/* ntGroup*/
        // add
        pnode.push(node)
        nodeMap.set(node.id, node)
        // next
        table.next()
      }
      // 用户
      var tabler = new SQLTable(vPar.getTable('k_flow_info'))
      tabler.first()
      while (!tabler.eof()) {
        var pnoder = Global.getTreeviewPNode(nodeMap, nodes, tabler.getValueByName('PID'))
        var nodeR = {}
        // data
        nodeR['id'] = tabler.getValueByName('FlowID')
        nodeR['name'] = tabler.getValueByName('FlowName')
        nodeR['text'] = tabler.getValueByName('FlowName')
        nodeR['icon'] = 'flow'
        nodeR['type'] = 1/* ntUser*/
        // add
        pnoder.push(nodeR)
        nodeMap.set(nodeR.id, nodeR)
        // next
        tabler.next()
      }
      // this.treeDefaultCheckedKeys = []
      this.treeData = nodes
      // this.treeDefaultCheckedKeys.push(JSON.parse(sessionStorage.getItem('saveFlowmgrIDs')))
      // this.formData[1].forEach(element => {
      //   this.treeDefaultCheckedKeys.push(element)
      // })
    },
    dialogClose() {
      this.checkVisible = false
    },
    dialogConfirm() {
    //   this.checkVisible = false
    //   this.ok(this.resule)
      // 批量删除
      if (this.formData[0] === 'Delete') {
        if (this.checkedNodes.length === 0) {
          this.$message({
            message: '请选择要删除的流程。',
            type: 'warning'
          })
          return
        }
        var nodesId = []// 分组ID
        var nodeIds = []// ID
        for (var i in this.checkedNodes) {
          if (this.checkedNodes[i].type === 0) {
            nodesId.push(this.checkedNodes[i].id)
          } else if (this.checkedNodes[i].type === 1) {
            nodeIds.push(this.checkedNodes[i].id)
          }
        }
        this.$refs.userconfirmlayer.open(parram => {
          var vPar = new ParamArr()
          vPar.setStrList('FlowID', nodeIds.join('\r\n'))
          vPar.setStrList('GroupID', nodesId.join('\r\n'))
          batchDelFlow(vPar).then(() => {
            this.checkVisible = false
            this.$parent.initWindow()
          })
        })
      }
      // 批量移动
      if (this.formData[0] === 'Move') {
        if (this.checkedNodes.length === 0) {
          this.$message({
            message: '请选择要移动的流程',
            type: 'warning'
          })
          return
        }
        var j = 0
        for (j in this.checkedNodes) {
          if (this.checkedNodes[j].type === 0) {
            this.nodeId = this.checkedNodes[j].id
          }
        }
        var nodeArr = []
        for (j in this.checkedNodes) {
          if (this.checkedNodes[j].type === 1) {
            nodeArr.push(this.checkedNodes[j].id)
          }
        }
        this.$set(this.selectParams, 'pid', this.nodeId)
        this.$set(this.selectParams, 'type', 'Flow')
        this.$set(this.selectParams, 'id', '')
        this.$refs.selectMoveGroup.open(parram => {
          var vPar = new ParamArr()
          vPar.setInt('Type', 1)
          vPar.setInt('Attachmode', 4)
          vPar.setStr('TargetID', parram.id)
          vPar.setStrList('FlowID', nodeArr.join('\r\n'))
          batchMoveFlow(vPar).then(() => {
            this.$parent.initWindow()
            this.checkVisible = false
          })
        })
      }
      if (this.formData[0] === 'edtMutexOpen') {
        this.checkedNodes.forEach(element => {
          if (element.icon === 'flow') {
            this.elementData.push(element)
          }
        })
        if (this.checkedNodes.length === 0 || this.elementData.length === 0) {
          this.$message({
            message: '请选择流程!',
            type: 'warning'
          })
          return
        }
        this.ok(this.elementData)
      }
    }
  }
}
</script>
